package wifiunion.com.notice.utils;


import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by fantao on 2015/9/15 0015.
 */
public class TimeUtil {
    private final static String TIME_FORMAT = "HH:mm";
    private final static String TIME_FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";
    private final static String TIME_FORMAT_YMD = "yyyy-MM-dd";
    private static long lastClickTime;
    private final static String TIME_FORMAT_YMD_2 = "yyyyMMdd";

    /**
     * 将时间显示为HH:mm:SS,注意HH不是24小时制也不是12小时制，是累计的小时数，可能是大于24，此方法主要用于播放器界面的时间显示
     *
     * @param millisecond 时间毫秒
     * @return HH:mm:SS
     */
    public static String formatForPlayer(long millisecond) {
        DecimalFormat df = new DecimalFormat("00");
        StringBuffer timeString = new StringBuffer();
        long hour = millisecond / (1000 * 60 * 60);
        long minute = millisecond % (1000 * 60 * 60) / (1000 * 60);
        long second = millisecond % (1000 * 60) / 1000;
        timeString.append(df.format(hour)).append(":");
        timeString.append(df.format(minute)).append(":")
                .append(df.format(second));
        return timeString.toString();
    }

    /**
     * 获取当前时间的小时和分钟
     *
     * @return HH:mm
     */
    public static String getCurFormatTime() {
        SimpleDateFormat df = new SimpleDateFormat(TIME_FORMAT);
        return df.format(new Date());
    }

    /**
     * 获取当前时间的yyyy-MM-dd HH:mm:ss格式
     *
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getCurYMDTime() {
        SimpleDateFormat df = new SimpleDateFormat(TIME_FORMAT_YMDHMS);
        return df.format(new Date());
    }


    /**
     * 获取当前时间的yyyy-MM-dd格式
     *
     * @return yyyy-MM-dd
     */
    public static String getYMDTime() {
        SimpleDateFormat df = new SimpleDateFormat(TIME_FORMAT_YMD);
        return df.format(new Date());
    }


    /**
     * 获取昨天 的yyyy-MM-dd格式
     * @return
     */
    public static String getYesterdayYMDTime() {
        SimpleDateFormat df = new SimpleDateFormat(TIME_FORMAT_YMD);
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        Date time = cal.getTime();
        return df.format(time);
    }

    /**
     * 将"yyyy-MM-dd HH:mm:ss"格式的日期转为long类型的time
     *
     * @param timeString
     * @return long类型的time
     */
    public static long getCommonTime(String timeString) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMDHMS);
        try {
            Date d = sdf.parse(timeString);
            return d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * 将"yyyy-MM-dd格式的日期转为long类型的time
     *
     * @param timeString
     * @return long类型的time
     */
    public static long getYMDCommonTime(String timeString) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMDHMS);
        try {
            Date d = sdf.parse(timeString);
            return d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * 将日期转换为yyyy-MM-dd HH:mm:ss格式字符串
     *
     * @param date 日期
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getDateString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMDHMS);
        return sdf.format(date);
    }


    /**
     * 将日期转换为yyyy-MM-dd格式字符串
     *
     * @param date 日期
     * @return yyyy-MM-dd
     */
    public static String getYMDString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMD);
        return sdf.format(date);
    }


    /**
     * 将日期转换为yyyy-MM-dd格式字符串
     *
     * @param date 日期
     * @return yyyyMMdd
     */
    public static String getYMDString2(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT_YMD_2);
        return sdf.format(date);
    }

    /**
     * 得到某年某月的第一天
     */
    public static String getFirstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    }

    /**
     * 得到某年某月的最后一天
     */
    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, value);
        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    }

    public static int compare_date(String DATE1, String DATE2) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }


    public static long getTimeCommonTime(String timeString) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);
        try {
            Date d = sdf.parse(timeString);
            return d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
